/*
********************************************************************************
* Copyright (C) 2021, xiang.D <dx_65535@163.com>.
* All right reserved.
*
* File Name   : user_api.cc
* Author      :
* Version     : V1.0
* Description :
* Journal     : 2021-12-17 version v1.0
* Brief       : Blog: https://blog.csdn.net/qq_38750572?spm=1001.2014.3001.5343
* Others      :
                Compile command: make
********************************************************************************
*/
#include "elevator.h"

#define ELEVATOR_LOGI(fmt, args...) printf("%d Elevator I: " fmt, __LINE__, ##args)
#define ELEVATOR_LOGD(fmt, args...) printf("%d Elevator D: " fmt, __LINE__, ##args)
#define ELEVATOR_LOGW(fmt, args...) printf("%d Elevator W: " fmt, __LINE__, ##args)
#define ELEVATOR_LOGE(fmt, args...) printf("%d Elevator E: " fmt, __LINE__, ##args)
#define ELEVATOR_LOG(fmt, args...)  printf(fmt, ##args)

CElevator::CElevator()
{
    Init();
}

CElevator::~CElevator()
{

}

CElevator* CElevator::GetInstance()
{
    static CElevator mInstance;
    return &mInstance;
}

void CElevator::Init()
{
    theElevatorSrv = CElevatorSrv::GetInstance();
    if (NULL == theElevatorSrv) {
        ELEVATOR_LOGE("Get Instance Failed!\n");
    }
}

int CElevator::TakeUpOrder(int floor)
{
    mMsgInfo = new SMsgPacket;
    mMsgInfo->content = new STakeOrderInfo;

    ((STakeOrderInfo *)(mMsgInfo->content))->direct = TAKE_UP;
    ((STakeOrderInfo *)(mMsgInfo->content))->floor = floor;
    mMsgInfo->type = SIG_ID_TAKE_UP_ORDER;
    theElevatorSrv->SendCmd(mMsgInfo);

    // TODO: 可优化为elevator接收消息后统一销毁
    delete ((STakeOrderInfo *)mMsgInfo->content);
    delete mMsgInfo;
    return 0;
}

int CElevator::TakeDownOrder(int floor)
{
    mMsgInfo = new SMsgPacket;
    mMsgInfo->content = new STakeOrderInfo;

    ((STakeOrderInfo *)(mMsgInfo->content))->direct = TAKE_DOWN;
    ((STakeOrderInfo *)(mMsgInfo->content))->floor = floor;
    mMsgInfo->type = SIG_ID_TAKE_DOWN_ORDER;
    theElevatorSrv->SendCmd(mMsgInfo);

    // TODO: 可优化为elevator接收消息后统一销毁
    delete ((STakeOrderInfo *)mMsgInfo->content);
    delete mMsgInfo;
    return 0;
}

int CElevator::ArriveFloor(int floor)
{
    mMsgInfo = new SMsgPacket;
    mMsgInfo->content = new int;

    *(int *)(mMsgInfo->content) = floor;

    mMsgInfo->type = SIG_ID_ARRIVE_FLOOR;
    theElevatorSrv->SendCmd(mMsgInfo);

    // TODO: 可优化为elevator接收消息后统一销毁
    delete ((int *)mMsgInfo->content);
    delete mMsgInfo;
    return 0;
}

